
"""
generate png text output
"""

from skimpyGimpy import canvas
from whiff import middleware
from whiff import resolver
from whiff import whiffenv

# FOR TESTING: SET TO NONE WHEN NOT TESTING
#DUMPFILEPATH = "/tmp/dump.png"
DUMPFILEPATH = None

def pngText(env, start_response):
    env = resolver.process_cgi(env, parse_cgi=True)
    cgi_parameters = env[whiffenv.CGI_DICTIONARY]
    text = cgi_parameters["text"][0]
    c = canvas.Canvas()
    c.setColor(99,99,0xff)
    propellpath = middleware.data_file_path("propell.bdf")
    c.addFont("propell", propellpath)
    c.setFont("propell", 2.0, 1.3)
    c.addText(0,0, text)
    imagedata = c.dumpToPNG(DUMPFILEPATH)
    start_response("200 OK", [('Content-Type', 'image/png')])
    return [imagedata]

__wsgi__ = pngText

def test():
    from whiff.middleware import misc
    env = {
        "wsgi.url_scheme" : "http",
        "PATH_INFO" : "/whatever",
        "QUERY_STRING" : "text=Hello+png+world",
        "REMOTE_ADDR" : "127.0.0.1",
        "REMOTE_HOST" : "localhost",
        "REQUEST_METHOD" : "GET",
        "SCRIPT_NAME" : "",
        "SERVER_NAME" : "localhost",
        "SERVER_PORT" : "8888",
        "SERVER_PROTOCOL" : "HTTP/1.1",
        "SERVER_SOFTWARE" : "WSGIServer/0.1 Python/2.5",
        }
    out = pngText(env, misc.ignore)
    txt = "".join(list(out))
    #pr repr(txt)
    if DUMPFILEPATH:
        print "test dumped to", DUMPFILEPATH

if __name__=="__main__":
    test()

